﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;

namespace ListOfTale
{
    /// <summary>
    /// Interaction logic for AddAuth.xaml
    /// </summary>
    public partial class AddAuth : Window
    {
        private bool IsNew { get; set; }
        private string lang { get; set; }
        private int id { get; set; }
        public AddAuth(bool IsNew,string name,string lang,int id)
        {
            InitializeComponent();
            this.IsNew = IsNew;
            if (!IsNew)
            {
                this.Name = name;
                this.lang = lang;
                this.id = id;
                NameTextBox.Text = name;
            }
        }
        public AddAuth(bool IsNew)
            : this(IsNew, "", "", -1)
        {

        }
        private void SaveBtn_Click(object sender, RoutedEventArgs e)
        {
            try
            {
                TalesDataSetTableAdapters.AuthorTableAdapter at = new TalesDataSetTableAdapters.AuthorTableAdapter();
                if (IsNew)
                {
                    var lastint = at.GetNewId();
                    if (at.Insert("", NameTextBox.Text, Convert.ToInt32( lastint)) == 1)
                    {
                        MessageBox.Show("Данные успешно сохранены");
                    }
                    else
                    { MessageBox.Show("Данные не сохранены"); }
                }
                else
                {
                    if (at.Update("", NameTextBox.Text,id, lang, Name, id) == 1)
                    {
                        MessageBox.Show("Данные успешно обновлены");
                    }
                    else
                    { MessageBox.Show("Данные не обновлены"); }
                }
                this.DialogResult = true;
                Close();
            }
            catch (Exception ex) { MessageBox.Show(ex.Message); }
        }

        private void NotSaveBtn_Click(object sender, RoutedEventArgs e)
        {
            this.DialogResult = false;
            Close();
        }
    }
}
